package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;

    @Operation(summary = "添加购物车")
    @RequiresLogin //请求权限: 需用户登录
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return success();
    }

    @Operation(summary = "购物车列表查询")
    @RequiresLogin //请求权限: 需用户登录
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        List<CartInfo> carInfoList = cartService.cartList();
        return success(carInfoList);
    }

    @Operation(summary = "删除购物车商品")
    @RequiresLogin //请求权限: 需用户登录
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary = "更新选中商品状态")
    @RequiresLogin //请求权限: 需用户登录
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();
    }

    @Operation(summary = "完成购物车商品的全选")
    @RequiresLogin //请求权限: 需用户登录
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return success();
    }

    @Operation(summary = "清空购物车")
    @RequiresLogin //请求权限: 需用户登录
    @GetMapping("/clearCart")
    public AjaxResult clearCart() {
        cartService.clearCart();
        return success();
    }

    @Operation(summary = "查询用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/getCartCheckedList/{userId}")
    public R<List<CartInfo>> getCartCheckedList(@PathVariable("userId") Long userId) {
        return R.ok(cartService.getCartCheckedList(userId));
    }

    @Operation(summary = "更新用户购物车列表价格")
    @InnerAuth
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable("userId") Long userId) {
        Boolean updateCartPrice = cartService.updateCartPrice(userId);
        return R.ok(updateCartPrice);
    }

    @Operation(summary = "删除用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/deleteCartCheckedList/{userId}")
    public R<Boolean> deleteCartCheckedList(@PathVariable("userId") Long userId) {
        Boolean deleteCartChecked = cartService.deleteCartCheckedList(userId);
        return R.ok(deleteCartChecked);
    }
}